#include "GameMenu.h"
#include "NormalMenu.h"
#include "Resources.h"
#include "VisibleRect.h"
#include "SurviveMenu.h"


bool GameMenu::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(!BaseLayer::init());
		
		CCTextureCache *pCache = CCTextureCache::sharedTextureCache();
		//background
		/*CCSprite *pBg = CCSprite::createWithTexture(pCache->textureForKey(s_bg), CCRectMake(0, 412, 618, 412));
		CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
		float scale = screenSize.width / pBg->getContentSize().width;
		pBg->setScale(scale);
		pBg->setPosition(VisibleRect::center());
		this->addChild(pBg, zBG);*/
		addBg(kBG1);
		//menu
		CCTexture2D *pTexture1 = pCache->textureForKey(s_screens4);
		CCRect rect1 = CCRectMake(180, 190,	240, 60);
		CCSprite *pN1 = CCSprite::createWithTexture(pTexture1, rect1);
		CCSprite *pS1 = CCSprite::createWithTexture(pTexture1, rect1);
		CCSprite *pD1 = CCSprite::createWithTexture(pTexture1, rect1);
		CCTexture2D *pTexture2 = pCache->textureForKey(s_screens4);
		CCRect rect2 = CCRectMake(180, 270,	240, 60);
		CCSprite *pN2 = CCSprite::createWithTexture(pTexture2, rect2);
		CCSprite *pS2 = CCSprite::createWithTexture(pTexture2, rect2);
		CCSprite *pD2 = CCSprite::createWithTexture(pTexture2, rect2);
		CCTexture2D *pTexture3 = pCache->textureForKey(s_screens4);
		CCRect rect3 = CCRectMake(180, 340,	240, 60);
		CCSprite *pN3 = CCSprite::createWithTexture(pTexture3, rect3);
		CCSprite *pS3 = CCSprite::createWithTexture(pTexture3, rect3);
		CCSprite *pD3 = CCSprite::createWithTexture(pTexture3, rect3);
		CCMenuItemSprite *pItem1 = CCMenuItemSprite::create(pN1, pS1, pD1, this, menu_selector(GameMenu::onNewGame));
		CCMenuItemSprite *pItem2 = CCMenuItemSprite::create(pN2, pS2, pD2, this, menu_selector(GameMenu::onNewGame));
		CCMenuItemSprite *pItem3 = CCMenuItemSprite::create(pN3, pS3, pD3, this, menu_selector(GameMenu::onExit));
		pItem1->setScale(1.5f);
		pItem2->setScale(1.5f);
		pItem3->setScale(1.5f);
		pItem1->setTag(kNormalMode);
		pItem2->setTag(kSurviveMode);
		pItem1->setPosition(ccpAdd(VisibleRect::center(), ccp(0, -200)));
		CCMenu *pMenu = CCMenu::create(pItem1, pItem2, pItem3, NULL);
		pMenu->setPosition(VisibleRect::bottom().x, VisibleRect::bottom().y + 250);
		pMenu->alignItemsVerticallyWithPadding(60);


		CCSprite *pPig = CCSprite::create(s_screens4, CCRectMake(460, 40, 330, 250));
		pPig->setScale(1.5f);
		pPig->setPosition(ccp(VisibleRect::rightTop().x - 200, VisibleRect::rightTop().y - 200));
		this->addChild(pPig, zPIG);

		this->addChild(pMenu, zMENU);
		
		bRet = true;
	} while (0);
	return bRet;
}
void GameMenu::keyBackClicked()
{
	CCDirector::sharedDirector()->end();
}
void GameMenu::onNewGame(CCObject *pSender)
{
	int tag = dynamic_cast<CCNode*>(pSender)->getTag();
	if (kNormalMode == tag)
	{
		REPLACE_SCENE(CCTransitionFade, 0.5f, NormalMenu);
	} else if (kSurviveMode == tag)
	{
		REPLACE_SCENE(CCTransitionFade, 0.5f, SurviveMenu);
	}
	//CCDirector::sharedDirector()->replaceScene(NormalMenu::scene());
}
void GameMenu::onExit(CCObject *pSender)
{
	CCDirector::sharedDirector()->end();
}